package com.jsyh.maidu.activity.me;

import android.content.Intent;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.jsyh.maidu.R;
import com.jsyh.maidu.activity.BaseActivity;
import com.jsyh.maidu.adapter.home.GoodsListMoreAdapter;
import com.jsyh.maidu.model.BestNewHot;
import com.jsyh.maidu.model.BestNewHotModel;
import com.jsyh.maidu.model.SuppinfoModel;
import com.jsyh.maidu.presenter.SuppInfoPresenter;
import com.jsyh.maidu.views.StoreView;

import java.util.List;

public class ShopGoodsListActivity extends BaseActivity implements View.OnClickListener, StoreView{

    TextView title;
    FrameLayout back;
    RecyclerView goodsList;
    GridLayoutManager layoutManager;
    GoodsListMoreAdapter adapter;
    String suppid;
    String cat_id;
    List<BestNewHot> data;
    SuppInfoPresenter presenter;
    String page = "1";

    RecyclerView.OnScrollListener scrollListener;

    @Override
    public void initView() {
        super.initView();
        setContentView(R.layout.activity_shop_good_list);
        title = (TextView) findViewById(R.id.title);
        back = (FrameLayout) findViewById(R.id.fl_Left);
        back.setOnClickListener(this);
        goodsList = (RecyclerView) findViewById(R.id.shop_goods_list);
        layoutManager = new GridLayoutManager(this, 2);
        goodsList.setLayoutManager(layoutManager);
        scrollListener = new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                boolean isBottom = goodsList.canScrollVertically(1);
                if (isBottom) {

                }
            }
        };


        goodsList.addOnScrollListener(scrollListener);
    }

    @Override
    public void initData() {
        super.initData();
        Intent intent = getIntent();
        suppid = intent.getStringExtra("suppid");
        cat_id = intent.getStringExtra("cat_id");
        if (!TextUtils.isEmpty(suppid) && TextUtils.isEmpty(cat_id)){
            presenter = new SuppInfoPresenter(this, this);
            presenter.getShopGoodsList(suppid, page);
        }else if(!TextUtils.isEmpty(suppid) && !TextUtils.isEmpty(cat_id)){
            presenter = new SuppInfoPresenter(this, this);
            presenter.getShopCatGoodsList(suppid,cat_id, page);
        }
    }

    @Override
    public void initTitle() {
        super.initTitle();
        title.setText("全部商品");
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.fl_Left:
                finish();
                break;
        }
    }

    @Override
    public void returnSuppInfo(SuppinfoModel model) {

    }

    @Override
    public void getShopGoodsList(BestNewHotModel model) {
        if (model.getCode().equals("1")){
            if (model.getData().size() > 0){
                if (Integer.valueOf(page) == 1){
                    data = model.getData();
                    adapter = new GoodsListMoreAdapter(this, data, "");
                    goodsList.setAdapter(adapter);
                }else if (Integer.valueOf(page) > 1){
                    data.addAll(model.getData());
                    adapter.notifyDataSetChanged();
                }

            }
        }
    }
}
